package com.zhou.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.zhou.constant.MessageConstant;
import com.zhou.entity.PageResult;
import com.zhou.entity.QueryPageBean;
import com.zhou.entity.Result;
import com.zhou.pojo.Order;
import com.zhou.pojo.Setmeal;
import com.zhou.service.OrderListService;
import com.zhou.service.OrderSettingService;
import com.zhou.service.SetmealService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderlist")
public class OrderSettingListController {
    @Reference
    private OrderListService orderListService;

    @Reference
    private SetmealService setmealService;

    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderListService.pageQuery(queryPageBean.getCurrentPage(), queryPageBean.getPageSize(), queryPageBean.getQueryString());
        return pageResult;
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Map map) {
        try {
            map.put("orderType", Order.ORDERTYPE_TELEPHONE);
            Result result = orderListService.order(map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ORDERLIST_FAIL);
        }
    }

    /**

     - 用于弹出编辑框 ,返回对应id的订单 和 所有的体检套餐
     *
     - @param id
     - @return
     */

    @RequestMapping("/findByIdWithAllSetmeal")
    public Result findByIdWithAllSetmeal(Integer id) {
        try {
            Map map = orderListService.findById(id);
            List<Setmeal> setmeals = setmealService.findAll();
            Map data = new HashMap();
            data.put("map", map);
            data.put("setmeals", setmeals);

            return new Result(true, MessageConstant.QUERY_ORDERLIST_SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDERLIST_FAIL);
        }
    }

    /**

     - 编辑预约订单
     *
     - @param order
     - @return
     */

    @RequestMapping("/edit")
    public Result edit(@RequestBody Order order) {
        return orderListService.edit(order);
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            orderListService.deleteById(id);
            return new Result(true, MessageConstant.DELETE_ORDERLIST_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDERLIST_FAIL);
        }
    }


}
